package com.rhok.utils;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;

public class LocationUtils {

	public static GeoPoint locationToPoint(Location location) {
		return new GeoPoint((int) (location.getLatitude() * 1E6),
				(int) (location.getLongitude() * 1E6));
	}
	
	public static interface MyLocationListener { public void locationReceived(Location location); }
	
	public static void waitForLocation(final MyLocationListener listener, Context context) {
		final LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

		final boolean[] foundLocation = new boolean[1];
		LocationListener onLocationChnanged = new LocationListener() {

			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onLocationChanged(Location location) {

				if (!foundLocation[0]) {
					foundLocation[0] = true;
					listener.locationReceived(location);
				}
				lm.removeUpdates(this);

			}
		};

		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 500,
				onLocationChnanged, null);
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 500,
				onLocationChnanged, null);
	}
}
